import logging
from shutil import copyfile

from utils.sql_utils.set import user_data, database, cursor, user_temp


def get_user_id(email: str) -> int:
    """
    获取用户id
    :param email:
    :return:
    """
    database.connect()
    cursor.execute(f'SELECT uid FROM {user_data} WHERE uemail = "{email}"')
    data = cursor.fetchall()
    return data[0][0]

def get_user_info(email: str) -> dict:
    """
    获取用户信息
    :param email:
    :return:
    """
    database.connect()
    cursor.execute(f'SELECT uname, password, level, headurl, uid FROM {user_data} WHERE uemail = "{email}"')
    data = cursor.fetchall()
    user_dick = {
        'uname': data[0][0],
        'password': data[0][1],
        'email': email,
        'level': data[0][2],
        'head': data[0][3],
        'uid': data[0][4]
    }
    return user_dick


def save_user_info(uname: str, password: str, email: str) -> bool:
    database.connect()
    try:
        cursor.execute(
            f'update {user_data} set uname = "{uname}", password = "{password}" where uemail = "{email}";')
        database.commit()
    except Exception as e:
        print(e)
        return False
    return True


def get_user_ks(email: str) -> dict:
    """
    获取用户科师空间信息
    :param email: 邮箱
    :return: 字典
    :example: {'ksname': '账号', 'kspassword': '密码', 'ksisok': 0, 'botopen': 0}
    """
    database.connect()
    cursor.execute(f'SELECT ksname, kspassword, ksisok, botopen, t1, t2, emailopen FROM {user_data} WHERE uemail = "{email}"')
    data = cursor.fetchall()
    user_dick = {
        'ksname': data[0][0],
        'kspassword': data[0][1],
        'ksisok': data[0][2],
        'botopen': data[0][3],
        't1': data[0][4],
        't2': data[0][5],
        'emailopen': data[0][6],
    }
    return user_dick


def save_user_ks(ksname: str, kspassword: str, botopen: str, email: str, t1: str = '06:30', t2: str = '13:30', emailopen=1) -> bool:
    database.connect()
    try:
        cursor.execute(f'update {user_data} set ksname = "{ksname}", kspassword = "{kspassword}", botopen = {botopen},'
                       f' t1 = "{t1}", t2 = "{t2}", emailopen = {emailopen}'
                       f' where uemail = "{email}";')
        database.commit()
    except Exception as e:
        print(e)
        return False
    return True

# 老版逻辑 已弃用
# def change_user_head(email: str, url: str):
#     database.connect()
#     try:
#         cursor.execute(
#             f'update {user_data} set headurl = "{url}" where uemail = "{email}";')
#         database.commit()
#     except Exception as e:
#         print(e)
#         return False
#     return True


base_url = 'D:/project/python/sympathy-back/static/user/compress_img'
# base_url = '/main/sympathy/website/sympathy-back/static/user/compress_img'
source_head = base_url + '/head/'
target_head = base_url + '/user/'
def change_user_head(uid: int, url: str):
    try:
        copyfile(source_head+url, target_head+str(uid)+'.jpg')
    except Exception as e:
        print(e)
        return False
    return True


def get_user_temp(uid: int):
    try:
        database.connect()
        cursor.execute(
            f'SELECT ttime, isnoon, temp FROM {user_temp} WHERE uid = {uid} order by ttime desc limit 30;')
        data = cursor.fetchall()
        user_list = []
        for _ in data:
            user_list.append(
                {
                    'time': _[0],
                    'isnoon': _[1],
                    'temp': _[2],
                }
            )
    except Exception as e:
        logging.error(e)
        return []
    return user_list


if __name__ == '__main__':
    # d = get_user_ks('3488636116@qq.com')
    d = get_user_temp(11)
    print(d)
    print(type(d))
